import sys
from PySide6.QtCore import QMetaObject, Qt
from PySide6.QtGui import QFont, QPixmap, QResizeEvent
from PySide6.QtWidgets import QApplication, QLabel, QSizePolicy, QVBoxLayout, QWidget
from qfluentwidgets import ScrollArea, ImageLabel, BodyLabel
from Resources import testphotos_rc


# 遗留的问题：仅横向或纵向缩放页面会导致图片失真
class ResultView(QWidget):
    def __init__(self):
        super().__init__()
        self.imageAreaContents = None
        self.imageArea = None
        self.userInfo = None
        self.verticalLayout = None
        self.imageLabels = []  # 存储所有的图片标签
        self.setupUi(self)

    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        self.setContentsMargins(0, 0, 0, 0)
        sizePolicy = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        self.verticalLayout = QVBoxLayout(Form)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.userInfo = QLabel(Form)
        self.userInfo.setObjectName(u"label")
        self.userInfo.setText("User And Date Info")
        font = QFont()
        font.setPointSize(14)
        self.userInfo.setFont(font)
        self.userInfo.setAlignment(Qt.AlignmentFlag.AlignHCenter)

        self.verticalLayout.addWidget(self.userInfo)

        self.imageArea = ScrollArea(Form)
        self.imageArea.setObjectName(u"scrollArea")
        sizePolicy1 = QSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Expanding)
        sizePolicy1.setHorizontalStretch(0)
        sizePolicy1.setVerticalStretch(0)
        sizePolicy1.setHeightForWidth(self.imageArea.sizePolicy().hasHeightForWidth())
        self.imageArea.setSizePolicy(sizePolicy1)
        self.imageArea.setWidgetResizable(True)
        self.imageArea.setAlignment(Qt.AlignmentFlag.AlignCenter)

        # 创建一个 QWidget，放置图片
        self.imageAreaContents = QWidget()
        self.imageAreaContents.setObjectName(u"imageAreaContents")
        self.imageArea.setWidget(self.imageAreaContents)

        self.verticalLayout.addWidget(self.imageArea)

        # 垂直布局排列图片
        self.verticalLayoutForImages = QVBoxLayout(self.imageAreaContents)
        self.verticalLayoutForImages.setObjectName(u"verticalLayoutForImages")

        # 添加图片到布局中
        for i in range(5):  # 假设5张图片
            imageLabel = ImageLabel(self.imageAreaContents)
            imageLabel.setPixmap(QPixmap(f":/images/photos/image_{i + 1}.jpg"))  # 使用RCC
            imageLabel.setScaledContents(True)  # 使图片可以缩放
            imageLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)  # 居中对齐
            self.imageLabels.append(imageLabel)
            self.verticalLayoutForImages.addWidget(imageLabel)

        self.imageArea.setWidget(self.imageAreaContents)

        QMetaObject.connectSlotsByName(Form)

    def resizeEvent(self, event: QResizeEvent):
        # 按比例调整图片大小
        scrollAreaWidth = self.imageArea.width()
        for label in self.imageLabels:
            pixmap = label.pixmap()
            if pixmap:
                scaledPixmap = pixmap.scaledToWidth(scrollAreaWidth, Qt.TransformationMode.SmoothTransformation)
                label.setPixmap(scaledPixmap)
        super().resizeEvent(event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = ResultView()
    ui.show()
    sys.exit(app.exec())
